﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using formats;

namespace Wiko_Placemarks
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Splash splash;
        AboutBox1 about;
        public List<plm> placemarks;
        int selectedIndex;
        private void Form1_Load(object sender, EventArgs e)
        {
            //this.Height = 0;
            //this.Width = 0;

            splash = new Splash();
            about = new AboutBox1();
            splash.Show();
            timer3.Enabled = true;

            placemarks = new List<plm>();
            selectedIndex = -1;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            splash.Opacity -= 0.01;
            this.Opacity += 0.01;
            if (splash.Opacity.Equals(0))
            {
                timer1.Enabled = false;
                timer2.Enabled = true;
                splash.Close();
            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            //int width = 640, height = 480;

            //if (this.Width < width) this.Width++;
            //if (this.Height < height) this.Height++;
            //if ((this.Width == width) && (this.Height == height))
            //{
                timer2.Enabled = false;
                status.Visible = true;
            //}
        }

        private void timer3_Tick(object sender, EventArgs e)
        {
            timer3.Enabled = false;
            timer1.Enabled = true;
            timer2.Enabled = true;
        }

        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
            List<plm> temp = new List<plm>();
            status.Text = "Opening";
            if(openFileDialog1.FileName.EndsWith(".kml"))
            {
                temp = new kml().read(openFileDialog1.OpenFile());
            }

            else if(openFileDialog1.FileName.EndsWith(".lmx"))
            {
                temp = new lmx().read(openFileDialog1.OpenFile());
            }

            if (temp == null) new message("Error", "Cannot load file, invalid file format.", 0).Show();

            else
            {
                placemarks.AddRange(temp);
                list.Items.Clear();
                foreach (plm x in placemarks)
                {
                    list.Items.Add(x.name, true);
                }

                list.Enabled = true;
                button2.Enabled = true;
                label11.ReadOnly = false;
                label12.ReadOnly = false;
                label13.ReadOnly = false;
                label14.ReadOnly = false;
                label15.ReadOnly = false;
                new message("Added", temp.Count.ToString()+" placemarks added successfully" , 1).Show();
            }

            status.Text = "Ready";
            
        }

        private void splitContainer1_SplitterMoved(object sender, SplitterEventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            saveFileDialog1.ShowDialog();
        }

        private void newCollectionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            placemarks.Clear();
            list.Items.Clear();
            list.Enabled = false;
            button2.Enabled = false;
            label11.ReadOnly = true;
            label11.Text = "<unavailable>";
            label12.ReadOnly = true;
            label12.Lines = new String[] {"<unavailable>","","", "Select a placemark to view its details"};
            label13.ReadOnly = true;
            label13.Text = "<unavailable>";
            label14.ReadOnly = true;
            label14.Text = "<unavailable>";
            label15.ReadOnly = true;
            label15.Text = "<unavailable>";
        }

        private void list_SelectedIndexChanged(object sender, EventArgs e)
        {
            selectedIndex = list.SelectedIndex;
            label11.Text = placemarks.ElementAt(list.SelectedIndex).name;
            label12.Text = placemarks.ElementAt(list.SelectedIndex).description;
            label13.Text = placemarks.ElementAt(list.SelectedIndex).longitude;
            label14.Text = placemarks.ElementAt(list.SelectedIndex).latitude;
            label15.Text = placemarks.ElementAt(list.SelectedIndex).altitude;


        }

        private void label22_Click(object sender, EventArgs e)
        {

        }

        private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
            if (saveFileDialog1.FileName.EndsWith(".kml"))
            {
                if(new kml().write(saveFileDialog1.OpenFile(), toBeSaved()))
                    new message("Saved", "File saved successfully", 2).Show();
                else new message("Failed", "Could not save file" , 0).Show();
            }

            else if (saveFileDialog1.FileName.EndsWith(".lmx"))
            {
                if(new lmx().write(saveFileDialog1.OpenFile(), toBeSaved()))
                    new message("Saved", "File saved successfully", 2).Show();
                else new message("Failed", "Could not save file", 0).Show();
            }

            else new message("Failed", "Invalid file format", 0).Show();

        }

        List<plm> toBeSaved()
        {
            List<plm> toSave = new List<plm>();
            for (int i = 0; i < placemarks.Count; i++)
            {
                if(list.GetItemChecked(i))
                    toSave.Add(placemarks[i]);
            }
            return toSave;
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (about.IsDisposed) about = new AboutBox1();
            about.Show();
            about.Focus();
        }

        private void label11_TextChanged(object sender, EventArgs e)
        {
            if(list.SelectedIndex>=0)
            placemarks[list.SelectedIndex].name = label11.Text;
        }

        private void label12_TextChanged(object sender, EventArgs e)
        {
            if (list.SelectedIndex >= 0)
            placemarks[list.SelectedIndex].description = label12.Text;
        }

        private void label13_TextChanged(object sender, EventArgs e)
        {
            if (list.SelectedIndex >= 0)
            placemarks[list.SelectedIndex].longitude = label13.Text;
        }

        private void label14_TextChanged(object sender, EventArgs e)
        {
            if (list.SelectedIndex >= 0)
            placemarks[list.SelectedIndex].latitude = label14.Text;
        }

        private void label15_TextChanged(object sender, EventArgs e)
        {
            if (list.SelectedIndex >= 0)
            placemarks[list.SelectedIndex].altitude = label15.Text;
        }
    }
}
